package hot.pay.office.service.impl;

import hot.pay.office.dao.CustomerMapper;
import hot.pay.office.model.entity.Customer;
import hot.pay.office.service.ICustomerService;
import hot.pay.office.service.IReadExcelService;
import hot.pay.utils.MethodUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@Service("CustomerServiceImpl")
public class CustomerServiceImpl implements ICustomerService {

	@Autowired
	private CustomerMapper customerMapper;


	@Autowired
	private IReadExcelService readExcelService;

	/***
	 * 保存客户信息
	 * @param customer
	 * @return
	 */
	@Override
	public Integer save(Customer customer) {
		return customerMapper.insert(customer);
	}

	/***
	 * 通过主键获取客户详情
	 * @param id
	 * @return
	 */
	@Override
	public Customer get(Long id) {
		return customerMapper.selectByPrimaryKey(id);
	}

	@Override
	public boolean importCustom(String path) throws Exception {
		Map<String,String> names = new HashMap<>();
		names.put("用户编号","addressnum");
		names.put("地址描述","addressdesc");
		names.put("小区id","communityid");
		names.put("小区名称","communityname");
		names.put("楼号","building");
		names.put("单元","unit");
		names.put("楼层","floor");
		names.put("楼门","room");
		names.put("户主","name");
		names.put("联系人","owner");
		names.put("联系电话","phone");
		names.put("身份证","cardid");
		names.put("是否有卡","iscard");
		names.put("卡号","cardno");
		names.put("建筑面积","buildingarea");
		names.put("取暖面积","heatingarea");
		names.put("单价","price");
		names.put("换热站","heatstation");
		names.put("机组","crew");
		names.put("分户控制类型串联，并联","controller");
		names.put("客户类型0表民用-1商用","usertype");
		names.put("是否黑名单 0表示否，1表示是","isblack");
		names.put("用热状态 有效0 无效1","status");
		names.put("计费方式0表示面积，1表示计量","feesytle");
		names.put("入网时间","netdate");
		names.put("入网年度","netyear");
		names.put("计费方式0表示面积，1表示计量","feesytle");
		names.put("合同开始","contractstart");
		names.put("合同结束","comtractend");
		names.put("备注","comtractend");
		List<Map<String, Object>> readMap= readExcelService.read2Map(path);
		List<Customer> result = new ArrayList<>();
		for(Map<String, Object> row : readMap){
			Map<String, Object> newrow = new HashMap<>();
			for(Map.Entry<String,Object> entry : row.entrySet()){
				if(names.containsKey(entry.getKey().trim())){
					newrow.put(names.get(entry.getKey().trim()),entry.getValue());
				}
			}
			if(newrow.size()>0){
				Customer customer = (Customer)MethodUtils.convertMap2Bean(newrow,Customer.class);
				result.add(customer);
			}
		}

		customerMapper.insertList(result);

//  `createuser` varchar(20) NOT NULL COMMENT '操作人',
//  `createtime` timestamp NULL DEFAULT NULL COMMENT '操作时间',
//  `lastupdatetime` varchar(20) DEFAULT NULL COMMENT '最后修改时间',
//  `remark` varchar(200) DEFAULT NULL COMMENT '',

		return true;
	}
}
